using System;
using System.Drawing;
using System.Windows.Forms;

namespace SmartMedical.WinUI
{
    /// <summary>
    /// 智慧医疗系统主窗体
    /// </summary>
    public partial class MainForm : Form
    {
        #region 私有字段
        // 控件字段由Designer.cs文件自动生成
        #endregion

        #region 构造函数
        /// <summary>
        /// 初始化主窗体
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            InitializeMainForm();
        }
        #endregion

        #region 初始化方法
        /// <summary>
        /// 初始化主窗体的基本设置
        /// </summary>
        private void InitializeMainForm()
        {
            // 设置窗体属性
            this.Text = "智慧医疗管理系统";
            this.WindowState = FormWindowState.Maximized;
            this.StartPosition = FormStartPosition.CenterScreen;
            
            // 设置状态栏信息
            toolStripStatusLabelUser.Text = "当前用户：管理员"; // TODO: 从登录信息获取
            
            // 启动定时器更新时间
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;
            timer.Tick += Timer_Tick;
            timer.Start();
            
            UpdateStatusTime();
        }
        
        /// <summary>
        /// 更新状态栏时间显示
        /// </summary>
        private void UpdateStatusTime()
        {
            toolStripStatusLabelTime.Text = $"当前时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}";
        }
        #endregion

        #region 事件处理方法
        /// <summary>
        /// 定时器事件，更新时间显示
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            UpdateStatusTime();
        }
        
        /// <summary>
        /// 选病人菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemSelectPatient_Click(object sender, EventArgs e)
        {
            try
            {
                SelectPatientForm selectPatientForm = new SelectPatientForm();
                if (selectPatientForm.ShowDialog() == DialogResult.OK)
                {
                    // 获取选中的病人信息
                    var selectedPatient = selectPatientForm.SelectedRegistration;
                    if (selectedPatient != null)
                    {
                        MessageBox.Show($"已选择病人：{selectedPatient.PatientName}\n卡号：{selectedPatient.CardNo}\n挂单号：{selectedPatient.RegistrationNo}", 
                                      "选择成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开选择病人窗口失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 写病历菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemWriteMedicalRecord_Click(object sender, EventArgs e)
        {
            try
            {
                // 首先选择患者
                SelectPatientForm selectPatientForm = new SelectPatientForm();
                if (selectPatientForm.ShowDialog() == DialogResult.OK)
                {
                    var selectedPatient = selectPatientForm.SelectedRegistration;
                    if (selectedPatient != null)
                    {
                        // 打开病历书写窗体，传入患者信息
                        MedicalRecordForm medicalRecordForm = new MedicalRecordForm(
                            selectedPatient.PatientId, 
                            selectedPatient.PatientName);
                        
                        if (medicalRecordForm.ShowDialog() == DialogResult.OK)
                        {
                            MessageBox.Show("病历保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择一个患者", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开病历书写窗口失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 开中药菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemPrescribeChineseMedicine_Click(object sender, EventArgs e)
        {
            try
            {
                // 首先选择患者
                SelectPatientForm selectPatientForm = new SelectPatientForm();
                if (selectPatientForm.ShowDialog() == DialogResult.OK)
                {
                    var selectedPatient = selectPatientForm.SelectedRegistration;
                    if (selectedPatient != null)
                    {
                        // 打开开中药窗体，传入患者信息
                        PrescribeChineseMedicineForm prescribeForm = new PrescribeChineseMedicineForm(
                            selectedPatient.PatientId, 
                            selectedPatient.PatientName);
                        
                        if (prescribeForm.ShowDialog() == DialogResult.OK)
                        {
                            MessageBox.Show("中药处方保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择一个患者", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开开中药窗口失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 开西药菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemPrescribeWesternMedicine_Click(object sender, EventArgs e)
        {
            try
            {
                // 首先选择患者
                SelectPatientForm selectPatientForm = new SelectPatientForm();
                if (selectPatientForm.ShowDialog() == DialogResult.OK)
                {
                    var selectedPatient = selectPatientForm.SelectedRegistration;
                    if (selectedPatient != null)
                    {
                        // 打开开西药窗体，传入患者信息
                        PrescribeWesternMedicineForm prescribeForm = new PrescribeWesternMedicineForm(
                            selectedPatient.PatientId, 
                            selectedPatient.PatientName);
                        
                        if (prescribeForm.ShowDialog() == DialogResult.OK)
                        {
                            MessageBox.Show("西药处方保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择一个患者", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开开西药窗口失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 病人挂号菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemPatientRegistration_Click(object sender, EventArgs e)
        {
            try
            {
                PatientRegistrationForm registrationForm = new PatientRegistrationForm();
                if (registrationForm.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("病人挂号成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开病人挂号窗口失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 医疗卡办理菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemMedicalCardManage_Click(object sender, EventArgs e)
        {
            try
            {
                // 打开医疗卡办理窗体
                using (var medicalCardForm = new MedicalCardForm())
                {
                    var result = medicalCardForm.ShowDialog(this);
                    if (result == DialogResult.OK)
                    {
                        // 医疗卡办理成功后的处理
                        MessageBox.Show("医疗卡办理申请已提交！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开医疗卡办理窗体失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 门诊收费菜单项点击事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MenuItemOutpatientCharge_Click(object sender, EventArgs e)
        {
            try
            {
                // 打开门诊收费窗体
                using (var chargeForm = new OutpatientChargeForm())
                {
                    var result = chargeForm.ShowDialog(this);
                    if (result == DialogResult.OK)
                    {
                        // 收费成功后的处理
                        MessageBox.Show("收费操作完成！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开门诊收费窗体失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确定要退出系统吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
        #endregion
        
        #region TODO: 待实现的方法
        /// <summary>
        /// 加载用户权限（待实现）
        /// </summary>
        /// <param name="userId">用户ID</param>
        private void LoadUserPermissions(int userId)
        {
            // TODO: 根据用户权限显示/隐藏相应的菜单项
            // 1. 从后端API获取用户权限
            // 2. 根据权限控制菜单项的可见性
        }
        
        /// <summary>
        /// 打开子窗体（待实现）
        /// </summary>
        /// <param name="childForm">子窗体</param>
        private void OpenChildForm(Form childForm)
        {
            // TODO: 在主面板中打开子窗体
            // 1. 清除当前子窗体
            // 2. 设置新窗体为子窗体
            // 3. 显示在主面板中
        }
        #endregion
    }
}